package Client;

import java.io.*;
import java.net.*;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;

import Model.Company;
import Model.ManagerTime;
import Model.MyTime;
import Model.Patchcard;
import Model.User;
import Model.card;
import Model.card2;

public class Client {

	ObjectOutputStream oos;
	ObjectInputStream ois;
	final int LOGIN = 1001;
	final int REG = 1002;
	final int IPCI = 1003;
	final int UMA =1004;
	final int SETTIME = 1005;
	final int VIEWUSER = 1006;
	final int MANAGER = 1007;
	final int DELETEUSER = 1008;
	final int PATCH = 1009;
	final int APPLY = 1010;
	final int DETERMINE = 1011;
	final int AGREE = 1012;
	final int REFUSE = 1013 ;
	final int VIEWUSERCARD =1014;
	final int ADDDEPARTMENT = 1015;
	final int DELDEPARTMENT = 1016;
	final int UPDDEPARTMENT = 1017;
	
	
	Socket s;

	public Client() throws UnknownHostException, IOException {
		s = new Socket("127.0.0.1", 1234);
		oos = new ObjectOutputStream(s.getOutputStream());
		ois = new ObjectInputStream(s.getInputStream());
	}
	public User login(String uid, String upw, int uAttribute) throws IOException,ClassNotFoundException {

		oos.writeInt(LOGIN);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(upw);
		oos.flush();
		oos.writeInt(uAttribute);
		oos.flush();

        return (User) ois.readObject();

}
	public User register( String uid,String uname,String upw, int uAttribute, String udepartment,String usercomputerip)
	throws IOException, ClassNotFoundException {

		oos.writeInt(REG);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(upw);
		oos.flush();
		oos.writeInt(uAttribute);
		oos.flush();
		oos.writeUTF(uname);
		oos.flush();
		oos.writeUTF(udepartment);
		oos.flush();
		oos.writeUTF(usercomputerip);
		oos.flush();
         return (User) ois.readObject();
         

}
	
	public card IPClockIn(String uid,String uname, String today,String uip,String usercomputerip,int number) throws IOException, ClassNotFoundException{
		oos.writeInt(IPCI);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(uname);
		oos.flush();
		oos.writeUTF(today);
		oos.flush();
		oos.writeUTF(uip);
		oos.flush();
		oos.writeUTF(usercomputerip);
		oos.flush();
//		ois.readUTF();
//		oos.flush();
		return (card)ois.readObject();
		
		
	}
	public User manage( String uid,String uname,String upw, int uAttribute, String udepartment, String usercomputerip)
	throws IOException, ClassNotFoundException {

		oos.writeInt(UMA);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(uname);
		oos.flush();
		oos.writeUTF(upw);
		oos.flush();
		oos.writeInt(uAttribute);
		oos.flush();
		oos.writeUTF(udepartment);
		oos.flush();
		oos.writeUTF(usercomputerip);
		oos.flush();
         return (User) ois.readObject();

}
	public  ManagerTime settime( String stime)
	throws IOException, ClassNotFoundException {

		oos.writeInt(SETTIME);
		oos.flush();
		oos.writeUTF(stime);
		oos.flush();
		return (ManagerTime) ois.readObject();
	}
	@SuppressWarnings("unchecked")
	public ArrayList<User> viewData() throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		oos.writeInt(VIEWUSER);
		oos.flush();
		ArrayList<User> users = (ArrayList<User>) ois.readObject();
		return users;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<card2> viewusercard(String uid) throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		oos.writeInt(PATCH);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		ArrayList<card2> cards2 = (ArrayList<card2>) ois.readObject();
		return cards2;
	}
	@SuppressWarnings("unchecked")
	public ArrayList<card2> managerviewusercard(String wantday) throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		oos.writeInt(VIEWUSERCARD);
		oos.flush();
		oos.writeUTF(wantday);
		oos.flush();
		ArrayList<card2> cards2 = (ArrayList<card2>) ois.readObject();
		return cards2;
	}
	public void ManageUsers(String uid,String uname,String upw,String udepartment,String usercomputerip) throws IOException{
		oos.writeInt(MANAGER);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(uname);
		oos.flush();
		oos.writeUTF(upw);
		oos.flush();
		oos.writeUTF(udepartment);
		oos.flush();
		oos.writeUTF(usercomputerip);
		oos.flush();
	}
	public void DeleteUser(String uid) throws IOException {
		// TODO Auto-generated method stub
		oos.writeInt(DELETEUSER);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
	}
	public void UserApply(String uid,String uname,String usertime,String uip,String state,String reason) throws IOException{
		oos.writeInt(APPLY);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(uname);
		oos.flush();
		oos.writeUTF(usertime);
		oos.flush();
		oos.writeUTF(uip);
		oos.flush();
		oos.writeUTF(state);
		oos.flush();
		oos.writeUTF(reason);
		oos.flush();
	}
	@SuppressWarnings("unchecked")
	public ArrayList<Patchcard> Determineapply() throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		oos.writeInt(DETERMINE);
		oos.flush();
		ArrayList<Patchcard> patchcards = (ArrayList<Patchcard>) ois.readObject();
		return patchcards;
	}
	public void Agreeapply(String uid, String usertime) throws IOException {
		// TODO Auto-generated method stub
		oos.writeInt(AGREE);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(usertime);
		oos.flush();
	}
	public void Refuseapply(String uid, String usertime) throws IOException {
		// TODO Auto-generated method stub
		oos.writeInt(REFUSE);
		oos.flush();
		oos.writeUTF(uid);
		oos.flush();
		oos.writeUTF(usertime);
		oos.flush();
	}
	public void AddDepartment(String dnumber) throws IOException, ClassNotFoundException{
		oos.writeInt(ADDDEPARTMENT);
		oos.flush();
		oos.writeUTF(dnumber);
		oos.flush();
		
		
	}
	public void DELDepartment(String dnumber) throws IOException, ClassNotFoundException{
		oos.writeInt(DELDEPARTMENT);
		oos.flush();
		oos.writeUTF(dnumber);
		oos.flush();
		
	}
	public void UPDDepartment(String dnumber, String dname) throws IOException, ClassNotFoundException{
		oos.writeInt(UPDDEPARTMENT);
		oos.flush();
		oos.writeUTF(dnumber);
		oos.flush();
		oos.writeUTF(dname);
		oos.flush();
		
	}

	
}